const util = require('../../utils/util.js');
const api = require('../../config/api.js');
const user = require('../../utils/user.js');

//获取应用实例
const app = getApp();

Page({
  data: {
    quests: [],
    banner: [
      { 'url': 'http://a1.qpic.cn/psc?/V11Wd0PZ0JeMgw/GrOcyTMGUuKzAoepz4mFmkTjRCbXBgjvvBbOZbTqq3BBXWkVmYQdBuwkYpZ0S5o0RtxRO*WN9ONe3unqr0l30g!!/b&ek=1&kp=1&pt=0&bo=oAUgAwAAAAADF7Q!&tl=1&vuin=672414262&tm=1586412000&sce=60-2-2&rf=viewer_4'},
      { 'url': 'http://a1.qpic.cn/psc?/V11Wd0PZ0JeMgw/GrOcyTMGUuKzAoepz4mFmkTjRCbXBgjvvBbOZbTqq3BBXWkVmYQdBuwkYpZ0S5o0RtxRO*WN9ONe3unqr0l30g!!/b&ek=1&kp=1&pt=0&bo=oAUgAwAAAAADF7Q!&tl=1&vuin=672414262&tm=1586412000&sce=60-2-2&rf=viewer_4' },
      { 'url': 'http://a1.qpic.cn/psc?/V11Wd0PZ0JeMgw/GrOcyTMGUuKzAoepz4mFmkTjRCbXBgjvvBbOZbTqq3BBXWkVmYQdBuwkYpZ0S5o0RtxRO*WN9ONe3unqr0l30g!!/b&ek=1&kp=1&pt=0&bo=oAUgAwAAAAADF7Q!&tl=1&vuin=672414262&tm=1586412000&sce=60-2-2&rf=viewer_4' }
    ]
  },

  onShareAppMessage: function () {
    return {
      title: '关于一个小程序走向的调查',
      desc: '问卷调查',
      path: '/pages/quest/quest'
    }
  },

  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getIndexData();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  getIndexData: function () {
    let that = this;
    util.request(api.QuestList).then(function (res) {
      that.setData({
        quests: res.data
      });
    });
  },
  getQuest(e) {
    if (!app.globalData || !app.globalData.hasLogin) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    } else {
      let quest = e.currentTarget.dataset.info
      if (quest.unWrite === 0) {
        wx.showToast({
          title: '该问卷您已填写'
        })
      } else if (quest.status) {
        wx.showToast({
          title: '该问卷已结束'
        })
      } else if (quest.unWrite === 1) {
        let questId = quest.id
        wx.navigateTo({
          url: "/pages/questDetail/questDetail?questId=" + questId
        })
      }
    }
  },
  onLoad: function (options) {
    this.getIndexData();
  },
  onReady: function () {
    this.getIndexData();
    // 页面渲染完成
  },
  onShow: function () {
    this.getIndexData();
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
})